<?php
require('../../../../lib/base.php');
$base=new base('web');
$session=$base->library('session',array('build'=>false));
if(!$session->ISSESS()){
	$helper=$base->library('helper');
	$helper->SMessage(5,domain.'view/Login.php?back='.domain.'home/module/system.php',"no","登陆失效,5秒后转到登陆页");
	exit();
}
$data['menu']=$base->cache(array('tag'=>'web.header','time'=>86400));
$data['menu']=unserialize($data['menu']);
$data['HomeMenu']=$base->cache(array('tag'=>'web.home.menu','time'=>86400));
$data['HomeMenu']=unserialize($data['HomeMenu']);
$data['foot']=$base->cache(array('tag'=>'web.footer','time'=>86400));
$sort=$base->model('sort');
if($base->get('action')!=false){
	$data['ModalityKey']=$sort->GSort(array('where'=>array('label ='=>'modality','locked='=>0),'order'=>array('sequence'=>'desc')));
	$data['modalityMenu']=$sort->GTree($data['ModalityKey']);
	$data['LabelKey']=$sort->GSort(array('where'=>array('label ='=>'adcolumn','locked='=>0),'order'=>array('sequence'=>'desc')));
	$data['LabelMenu']=$sort->GTree($data['LabelKey']);
	$data['formatList']=$sort->GSort(array('where'=>array('label='=>'format','locked='=>0)));
	$data['board']=intval($base->get('board',true));
	if($data['board']!=0){
		$board=$base->model('billboard');
		$select=$board->GBoard(array('where'=>array('member ='=>$session->GVAR('member'),'id ='=>$data['board'])));
		if(!empty($select)){
			$select=reset($select);
			$data['name']=$select['name'];
			$data['format']=$select['format'];
			$data['usage']=$select['usage'];
			$data['modality']=$select['modality'];
			$data['adcolumn']=explode(',',$select['adcolumn']);
			if($base->get('action')=='code'){
				$config=$base->model('config');
				$data['config']=$config->GConfig(array('where'=>array('label ='=>'advertiser','name IN'=>array('server','verify'))));
				foreach($data['config'] as $key=>$value){$data['config'][$value['name']]=$value['value'];unset($data['config'][$key]);}
				$data['code']=true;
			}
		}
	}
	$data['submit']=$base->post('submit');
	if($data['submit']!==false){
		$data['message']=array('result'=>false,'msg'=>array());
		$data['name']=trim($base->post('name',true));
		$data['format']=intval($base->post('format'));
		$data['usage']=intval($base->post('usage'));
		$data['modality']=intval($base->post('modality'));
		$data['adcolumn']=array_filter(explode(',',$base->post('adcolumn')),'is_numeric');
		$time=$base->runtime;
		if($data['name']==''){
			$data['message']['msg'][]='广告位名称不可为空,这将有助于您管理的您的广告位.';
		}else if(count($data['adcolumn'])==0){
			$data['message']['msg'][]='您至少得添加一个标签.';
		}else if($data['modality']==0){
			$data['message']['msg'][]='请选择广告展示形式.';
		}else{
			$config=$base->model('config');
			$data['config']=reset($config->GConfig(array('where'=>array('label ='=>'advertiser','name='=>'server'))));
			$data['config']['server']=$data['config']['value'];
			$data['config']['verify']=$base->vars('app','encryption');
			$board=$base->model('billboard');
			$data['board']=intval($base->post('board'));
			if($data['board']!=0){
				$affected=$board->UBoard(array('set'=>array('name ='=>$data['name'],'format='=>$data['format'],'`usage`='=>$data['usage'],'modality='=>$data['modality']),'adcolumn='=>implode(',',$data['adcolumn']),'where'=>array('member ='=>$session->GVAR('member'),'id ='=>$data['board'])));
				$data['code']=true;
				$data['message']['result']=true;
				if($affected==1){
					$data['message']['msg'][]='广告位修改成功,你可以在&nbsp;<a href="'.domain.'home/module/system.php">智能广告</a>&nbsp;中查看.';
				}else{
					$data['message']['msg'][]='请务重复提交,这个广告位已经被您修改.';
				}
			}else{
				$affected=$board->GBoard(array('where'=>array('member ='=>$session->GVAR('member'),'name ='=>$data['name'],'format ='=>$data['format'],'modality ='=>$data['modality'])));
				if(empty($affected)){
					$data['board']=$board->IBoard(array('name'=>$data['name'],'format'=>$data['format'],'usage'=>$data['usage'],'modality'=>$data['modality'],'adcolumn'=>implode(',',$data['adcolumn']),'member'=>$session->GVAR('member'),'time'=>$time),1);
					if(empty($data['board'])){
						$data['message']['msg'][]='您输入的数据有误,请核实后再提交.';
					}else{
						$data['message']['result']=true;
						$data['message']['msg'][]='广告位添加成功.';
					}
				}else{
					$data['board']=reset($affected);
					$data['board']=$data['board']['id'];
					$data['message']['msg'][]='请务重复提交,这个广告位已添加到数据库中.';
				}
				$data['code']=true;
			}
		}
	}
	$base->set($data);
	$base->view('home/module/advertisers.php');
}else{
$data['label']=$sort->GSort(array('where'=>array('label IN'=>array('modality','format'),'locked='=>0)));
$billboard=$base->model('billboard');
$board=intval($base->get('board'));
if($board!=0){
	$billboard->UBoard(array('where'=>array('member ='=>$session->GVAR('member'),'id ='=>$board),'set'=>array('locked ='=>10)));
}
	$temp=$billboard->GBoard(array('where'=>array('member ='=>$session->GVAR('member'),'locked='=>0)));
	$pagination=$base->library('pagination',array('base_url'=>domain.'home/module/system.php?','total_rows'=>count($temp),'per_page'=>8,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
	$data['pagination']=$pagination->create_links();
	$page=intval($base->get('page',true));
	$data['board']=array_slice($temp,$page,8);
	$base->set($data);
	$base->view('home/module/system.php');
}
?>